/**
 * create by young (young2f@outlook.com) on 2020/9/16
 * 选择辅料
 */


'use strict';
import React from 'react';
import {Button, Card, Descriptions, Form, notification} from 'antd';

import FormBase from '../../../components/Base/FormBase';
import {connect} from "dva";
import moment from 'moment';
import 'moment/locale/zh-cn';

import {ModalFooter} from "antd-extra";
import DescriptionGroup from "../../../components/DescriptionGroup";
import ModelDescriptionGroup from "../../../components/ModelDescriptionGroup";
import ViewTitle from "../../../components/ViewTitle";


moment.locale('zh-cn');
const table = 'Receipt';
@Form.create()
@connect(({
            user,
            global,
            dynamicLabel,
            finance,
            store
          }) => ({...user, ...finance, ...dynamicLabel, ...global, ...store}))
export default class ModalReceiptRecordDetail extends FormBase {
  constructor(props) {
    super(props);
    this.state = undefined;
  }

  componentDidMount() {
    const {values = {}} = this.props;
    const {code} = values;
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table: 'Receipt'}
    });
    this.props.dispatch({
      type: 'finance/findRecord',
      payload: {code, table}
    });
  }

  renderFoot = () => {
    const {foot, value, modalRef, form, callback} = this.props;

    if (foot) {
      return foot(value, modalRef);
    } else {
      return (
        <ModalFooter>
          <Button onClick={() => {
            callback();
            modalRef.close();
          }}>关闭</Button>
        </ModalFooter>
      );
    }
  };
  getCard = (files) => {
    const temp = [];
    _.map(files, file => {
      temp.push(<Card
        hoverable
        style={{width: 640}}
        cover={<img alt="example" src={file.preview}/>}
      >

      </Card>);
    });
    return temp;
  };


  render() {
    const {style, record = {}, isReview = false, callback} = this.props;
    const {fileList = []} = record;
    const model = this.props["ReceiptModel"];

    const labels = [];
    _.map(model, (model, key) => {
      model.key = key;
      labels.push(model);
    });
    return (
      <div style={{...style}}>
        <ViewTitle
          title={"记录详情"}
          actions={[
            isReview && <Button type="primary" onClick={() => {
              record.reviewStatus = '同意';
              this.props.dispatch({
                type: 'finance/updateRecord',
                payload: record
              }).then(res => {
                this.componentDidMount();
                callback();
              });
            }}>同意</Button>,
            isReview && <Button onClick={() => {
              record.reviewStatus = '驳回';
              this.props.dispatch({
                type: 'finance/updateRecord',
                payload: record
              }).then(res => {
                this.componentDidMount();
                callback();
              });
            }}>驳回</Button>
          ]}
        ></ViewTitle>
        <ModelDescriptionGroup
          model={model}
          values={record}
        ></ModelDescriptionGroup>
        {
          this.getCard(fileList)
        }
        {this.renderFoot()}
      </div>

    );
  }
}

